import 'package:get/get.dart';
import 'package:xuance/bean/AiWarningitemBean.dart';
import 'package:xuance/bean/HomeAiWarnBean.dart';
import 'package:xuance/bean/TimeEnum.dart';
import 'package:xuance/bean/TrafficDetailDataBean.dart';
import 'package:xuance/bean/exp.dart';
import 'package:xuance/network/api/AiWarningApi.dart';
import 'package:xuance/network/api/CommonApi.dart';
import 'package:xuance/util/common_util.dart';
import 'package:xuance/util/toast_util.dart';

import 'state.dart';

class TrafficDetailBean {
  final AiWarningItemList bean;
  final bool isWarnDetail;
  final String queryCondition;
  final TimeEnum type;

  TrafficDetailBean(
      {required this.bean,
      required this.isWarnDetail,
      required this.queryCondition,
      required this.type});
}

class WarningDetailLogic extends GetxController {
  final WarningDetailState state = WarningDetailState();
  late TrafficDetailBean bean;
  late TimeEnum type;
  @override
  void onInit() {
    super.onInit();
    bean = Get.arguments as TrafficDetailBean;
    type = bean.type;
    state.queryCondition.value = bean.queryCondition;
    if (bean.isWarnDetail) {
      getChart(bean.bean.sip);
      getTcpFlow();
      state.sip.value = bean.bean.sip ?? "";
      state.dip.value = bean.bean.dip ?? "";
      state.spt.value = bean.bean.spt ?? "";
      state.dpt.value = bean.bean.dpt ?? "";
      state.startTime.value = bean.bean.startDate ?? "";
      state.endTime.value = bean.bean.endDate ?? "";
      state.flags.value =
          "ACK=${bean.bean.tcpFlagsAck}、SYN=${bean.bean.tcpFlagsSyn}、URG=${bean.bean.tcpFlagsUrg}、PSH=${bean.bean.tcpFlagsPsh}、RST=${bean.bean.tcpFlagsRst}、SYN-ACK=${bean.bean.tcpFlagsSynAck}";
      state.protocols.value = (bean.bean.protocols ?? []).join(",");
      state.uri.value = bean.bean.uri ?? "";
      state.bytes.value =
          "source=${bean.bean.sbytes}、source payload=${bean.bean.spayloadBytes}、destination=${bean.bean.dbytes}、destination payload=${bean.bean.dpayloadBytes}";
    } else {
      getArkimeDetail();
    }
  }

  Future<void> getArkimeDetail() async {
    try {
      TrafficDetailDataBean? data = await CommonApi.getTrafficDetail(bean.bean);
      getChart(data?.sourceip ?? "");
      state.tcpFlow.value = data?.pcapFlowText ?? "";
      state.sip.value = data?.sourceip ?? "";
      state.dip.value = data?.destinationip ?? "";
      state.spt.value = "${data?.sourceport}";
      state.dpt.value = "${data?.destinationport}";
      state.startTime.value = date2String(DateTime.fromMillisecondsSinceEpoch(
          data?.firstPacket?.toInt() ?? 0))!;
      state.endTime.value = date2String(
          DateTime.fromMillisecondsSinceEpoch(data?.lastPacket?.toInt() ?? 0))!;
      state.flags.value =
          "ACK=${data?.tcpflagsack}、SYN=${data?.tcpflagssyn}、URG=${data?.tcpflagsurg}、PSH=${data?.tcpflagspsh}、RST=${data?.tcpflagspsh}、SYN-ACK=${data?.tcpflagssynack}";
      state.protocols.value = data?.protocol?.join(",") ?? "";
      state.uri.value = data?.httpuri?.join(",") ?? "";
      state.bytes.value =
          "source=${data?.sourcebytes}、source payload=${data?.clientbytes}、destination=${data?.destinationbytes}、destination payload=${data?.serverbytes}";
    } catch (e) {
      if (e is ApiException) {
        if (e.code == 1) {
          showToast("获取详情数据失败");
        } else {
          showToast(e.message);
        }
      } else {
        showToast("$e");
      }
    }
  }

  Future<void> getTcpFlow() async {
    try {
      state.tcpFlow.value = await AiWarningApi.getTcpFlow(
          esId: bean.bean.esId ?? "",
          queryCondition: state.queryCondition.value);
    } catch (e) {
      print(e);
      print("获取es文档失败");
    }
  }

  Future<void> getChart(sip) async {
    try {
      var time = getTime();
      List<HomeAiWarnBean> list = await AiWarningApi.aiPredice(
          sip: sip,
          startTime: time[0],
          endTime: time[1],
          queryCondition: state.queryCondition.value);
      state.chartList.clear();
      state.chartList.addAll(list);
    } catch (e) {
      print("获取图形列表失败");
      print(e);
    }
  }

  List<String> getTime() {
    var now = DateTime.now();
    var startTime = "";
    switch (type) {
      case TimeEnum.hour:
        startTime = "${date2String(now.subtract(const Duration(hours: 1)))}";
        break;
      case TimeEnum.day:
        startTime = "${date2String(now.subtract(const Duration(hours: 24)))}";
        break;
      case TimeEnum.week:
        startTime = "${date2String(now.subtract(const Duration(days: 7)))}";
        break;
      default:
        startTime = "${date2String(now.subtract(const Duration(days: 30)))}";
    }
    String endTime = date2String(now)!;
    return [startTime, endTime];
  }
}
